import { Animation } from 'cc'
import { StateMachine, getInitParamTrigger } from "../../Base/StateMachine"
import { EnemyManager } from './EnemyManager'
import { State } from '../../Base/State'
import { StateTypeEnum } from '../../Common'

export class EnemyStateMachine extends StateMachine {

    entity: EnemyManager
    init(entity: EnemyManager) {
        this.animationComponent = this.node.addComponent(Animation)
        this.entity = entity
        this.initParam()
        this.initState()
    }

    initParam() {
        this.params.set(StateTypeEnum.idle, getInitParamTrigger())
        this.params.set(StateTypeEnum.run, getInitParamTrigger())
        this.params.set(StateTypeEnum.none, getInitParamTrigger())
        this.params.set(StateTypeEnum.impact, getInitParamTrigger())
    }

    initState() {
        this.stateMachines.set(StateTypeEnum.idle, new State(this, this.entity.type, StateTypeEnum.idle))
        this.stateMachines.set(StateTypeEnum.run, new State(this, this.entity.type, StateTypeEnum.run))
        this.stateMachines.set(StateTypeEnum.none, new State(this, this.entity.type, StateTypeEnum.none))
        this.stateMachines.set(StateTypeEnum.impact, new State(this, this.entity.type, StateTypeEnum.none))
    }

    run() {

        switch (this.currencyState) {
            case this.stateMachines.get(StateTypeEnum.idle):
            case this.stateMachines.get(StateTypeEnum.run):
            case this.stateMachines.get(StateTypeEnum.none):
                if (this.params.get(StateTypeEnum.idle).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.idle)
                } else if (this.params.get(StateTypeEnum.run).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.run)
                } else if (this.params.get(StateTypeEnum.none).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.none)
                } else if (this.params.get(StateTypeEnum.impact).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.none)
                }

                break;
            case this.stateMachines.get(StateTypeEnum.impact):
                if (this.params.get(StateTypeEnum.none).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.none)
                }
                break;
            default:
                this.currencyState = this.stateMachines.get(StateTypeEnum.run)
                break;
        }

    }

}